<template>
    <div>
       <h3>角色列表页面</h3>
       <Breadcrumb :breadcrumba="breadcrumba" :breadcrumbb="breadcrumbb"></Breadcrumb>

       <el-card class="box-card">
           <el-row :span="8">
               <el-button type="primary" @click="addRoles()">
                   添加角色
               </el-button>
           </el-row>
           <!-- 表格 -->
           <el-table
           :data="rolesList"
           border
           stripe
           size="mini"
           style="width: 100%">
           <!-- 展开列 -->
                <el-table-column
                align="center"
                type="expand"
                label="下拉"
                width="80"
                class="fff">
                    <template slot-scope="scope">
                        <el-row :class="['bdbottom', i1 === 0 ? 'bdtop' : '']" v-for="(item, i1) of scope.row.children" :key="item.id">
                            <!-- 渲染一级 -->
                            <el-col :span="5">
                                <el-tag closable @close="removeRightById(scope.row,item.id)">
                                    {{item.authName}}
                                </el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 渲染二级 和三级 -->
                            <el-col :span="19">
                                <el-row  :class="i2 === 0 ? '' : 'bdtop' " v-for="(ite,i2) of item.children" :key="ite.id" >
                                    <el-col :span="6">
                                        <el-tag  type="success" closable @close="removeRightById(scope.row,ite.id)">
                                            {{ite.authName}}
                                        </el-tag>
                                        <i class="el-icon-caret-right"></i>                          
                                    </el-col>
                                    <!-- 渲染三级 -->
                                    <el-col  :span="18">
                                        <!-- 传的参数 要传整条的 方便获取之后渲染 不关闭 -->
                                         <el-tag  type="warning" closable  v-for="it of ite.children" :key="it.id" @close="removeRightById(scope.row,it.id)">
                                                    {{it.authName}}
                                                </el-tag>
                                         <!-- <el-row >
                                            <el-col :span="10">
                                                <el-tag  type="warning" closable>
                                                    {{ite.authName}}
                                                </el-tag>
                                            </el-col> 
                                        </el-row>        -->
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                        <!-- <pre>
                            {{scope.row}}
                        </pre> -->
                    </template>
                </el-table-column>
           <!-- 索引 -->
                <el-table-column
                align="center"
                type="index"
                label="序号"
                width="80">
                </el-table-column>
                <el-table-column
                prop="roleName"
                label="角色名称"
                >
                </el-table-column>
                <el-table-column
                prop="roleDesc"
                label="角色描述"
                >
                </el-table-column>
                <el-table-column
                label="操作"
                >
                    <template slot-scope="scope">
                       <el-tooltip class="item" effect="dark" content="编辑" placement="top" :enterable="false">
                        <el-button type="primary" icon="el-icon-edit" @click="xiugai(scope.row.id)">编辑</el-button>
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger" icon="el-icon-delete" @click="delRoles(scope.row.id)">删除</el-button>    
                    </el-tooltip>
                    <el-tooltip class="item" effect="dark" content="分配权限" placement="top" :enterable="false">
                        <!-- scope.row 作为参数传递给函数 -->
                        <el-button type="warning" icon="el-icon-s-tools" @click="showSetRightDialog(scope.row)">分配权限</el-button>
                    </el-tooltip>
                    </template>
                </el-table-column>
           </el-table>
        </el-card>
        <!-- 分配权限弹出框 -->
        <el-dialog
        title="分配权限"
        :visible.sync="setRightDialogVisible"
        width="30%"
        :before-close="handleClose"
        @close="setRightDialogClosed">
       
            <span>主题区域</span>  
            <!-- 树形控件  node-key 选中的值是啥  是id-->
            <!-- 给tree加一个引用  方便调用它的函数 进行获取id -->
            <!-- props 是规则 label是每一项的数据 childern 是子菜单的分级字段 -->
            <el-tree :data="rightsList" :props="treeProps" show-checkbox  
            node-key="id" 
            default-expand-all
            :default-checked-keys="defkeys"
            ref="treeRef"></el-tree>
            
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRightDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Breadcrumb from "../../components/Breadcrumb"
import {getRolesList,delRolesRight,setRights} from "../../api/roles"
import {getRightsList} from "../../api/rights"
    export default {
        components:{
            Breadcrumb
        },
        data(){
            return{
                breadcrumba:'',
                breadcrumbb:'',
                rolesList:[],
                setRightDialogVisible:false,
                rightsList:[],
                treeProps:{
                    label:'authName',
                    children:'children'
                },
                defkeys:[],
                // 角色id
                roleId:''
                   
                
            }
        },
        created(){
            this.breadcrumba=sessionStorage.getItem('breadcrumba')
            this.breadcrumbb=sessionStorage.getItem('breadcrumbb')
            this.getRolesList()
        },
        methods:{
            addRoles(){
                console.log(1);
            },
            async getRolesList(){
                let {data:res} = await getRolesList()
                console.log(res,"获取的角色列表");
                this.rolesList = res.data
            },
            xiugai(){
                console.log(1);
            },
            delRoles(row){
                console.log(row);
            },
            // 删除权限
            async removeRightById(role,rightId){
                // console.log(roleId,rightId,"删除的id");
                // let {data:res} = await delRolesRight({roleId:roleId,rightId:rightId})
                // console.log(res,"删除角色某一个权限");
                const confirmResult = await this.$confirm("确定删除？",
                "提示",
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err)

                if(confirmResult !== 'confirm'){
                    return this.$message.info('取消了删除')
                }

                // console.log('确认了删除');
                console.log(role.id,rightId,"删除的id");
                let {data:res} = await delRolesRight({roleId:role.id,rightId:rightId})
                console.log(res,"删除角色某一个权限");
                if(res.meta.status !== 200){
                    return this.$message.error('删除权限失败')
                }
                this.$message.success('删除权限成功')
                // this.getRolesList()
                // 当前角色重新 授权 这样不会 重新渲染 表格
                role.children = res.data
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {});
            },
            async showSetRightDialog(role){
                
                this.roleId = role.id
         
                let {data:res} = await getRightsList('tree')
                console.log(res,"所有权限列表树状");

                if(res.meta.status !== 200){
                    return this.$message.error('删除权限失败')
                }

                this.rightsList= res.data

                // 调用 递归函数
                this.getLeafKeys(role,this.defkeys)
                this.setRightDialogVisible = true
            },
            // 通过递归来获取 或有三级节点 并保存到 defKeys数组中
            getLeafKeys(node,arr){
                // 如果 nonde 节点 不包含children 属性  则是三级节点
                if(!node.children){
                    return arr.push(node.id)
                }

                // 递归函数
                node.children.forEach(item =>
                    this.getLeafKeys(item,arr)
                )
            },
            // 监听分配权限对话框的
            setRightDialogClosed(){
                this.defkeys = []
            },
            // 分配权限  
            // 这里 接口 后台 有点问题 添加不上权限 解决了 得传的是个 对象 字段 是 rids 才行！
            async allotRights(){
                // 引用 $refs 获取所有全选 和 半选的 状态的id
                const keys = [
                    ...this.$refs.treeRef.getCheckedKeys(),
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ]
                const riosIdStr = keys.join(',')
                console.log(riosIdStr);
                console.log(this.roleId);
                // 
                let {data:res} = await setRights(this.roleId,{rids:riosIdStr})
                console.log(res,"角色授权返回值");
                if(res.meta.status !== 200){
                    return this.$message.error('分配权限失败')
                }
               
                this.$message.success('分配权限成功！')
                this.getRolesList()
                this.setRightDialogVisible = false
                
            }
        }
    }
</script>

<style scoped>
    .el-table{
        margin-top: 15px;
    }
    .el-tag{
        margin: 10px;
    }
    .bdtop{
        border-top: 1px solid #eee;
    }
    .bdbottom{
        border-bottom: 1px solid #eee;
        display: flex;
        align-items: center;
    }
    .fff{
        transition: 2s;
    }
</style>